


const ajax = obj => {
    const {
        type,
        data = {},
        path,
        cb,
        dataType = 'json'
    } = obj;

    let params = '';

    for (let key in data) {
        params += `${key}=${data[key]}&`;
    }
    //去掉最后的 &
    params = params.substring(0, params.length - 1);

    const xhr = new XMLHttpRequest();
    if (type.toLowerCase() === 'get') {
        xhr.open(type, path + '?' + params, true);
        xhr.send();

    }
    else {
        xhr.open(type, path, true);
        xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
        xhr.send(params);
    }

    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4 && xhr.status === 200) {
            let res = xhr.responseText;
            if (dataType.toLowerCase() === 'json') {
                res = JSON.parse(res);
            }
            cb && cb(res);
        }
    }
}


function queryString(name) {
    const url = location.search.replace('?', '');
    let arr = url.split('&');
    // console.log(url);
    // console.log(arr);
    arr = arr.map(v => {
        return {
            name: v.split('=')[0],
            content: v.split('=')[1]
        }
    })
    // console.log(arr)

    const res = arr.filter(v => v.name === name)[0];
    if (res) {
        return res.content;
    }
    return '';
}